<?php

/**
 * Description of offers
 *
 * @author Teis Lindemark
 */
/*$fav = Array();
if(isset($_POST)) {
	setFavorites($_POST['id']);
}*/

$db = new PGSQLConnector('u_superuser');

$offersSQL = "SELECT DISTINCT\n";
$offersSQL .= "o.idoffer AS idoffer,\n";
$offersSQL .= "o.offerref AS refno,\n";
$offersSQL .= "org.abbreviation AS abbreviation,\n";
$offersSQL .= "org.name AS name,\n";
$offersSQL .= "o.offertype AS offertype,\n";
$offersSQL .= "o.idorg_offeredby AS idorg,\n";
$offersSQL .= "o.durationminweeks AS durationminweeks,\n";
$offersSQL .= "o.durationmaxweeks AS durationmaxweeks,\n";
$offersSQL .= "o.datefrom AS periodfrom,\n";
$offersSQL .= "o.dateuntil AS perioduntil,\n";
$offersSQL .= "o.idiaeste_responsible AS idiaeste,\n";
$offersSQL .= "i.abbreviation AS iaeste,\n";
$offersSQL .= "cn.name AS country,\n";
$offersSQL .= "cn.countrycode AS countrycode\n";
$offersSQL .= "FROM offer AS o\n";
$offersSQL .= "JOIN org AS org ON o.idorg_offeredby = org.idorg\n";
$offersSQL .= "LEFT JOIN org AS i ON o.idiaeste_responsible = i.idorg\n";
$offersSQL .= "JOIN country AS cn ON org.countrycode = cn.countrycode\n";
$offersSQL .= "JOIN memo AS m ON m.idoffer = o.idoffer\n";
$offersSQL .= "WHERE o.offeryear = '" . getYear() . "'\n";
$offersSQL .= "AND org.countrycode <> '" . getCountry() . "'\n";
$offersSQL .= "AND o.offerref NOT LIKE '-%'\n";
$offersSQL .= "AND o.timedeleted IS NULL\n";
$offersSQL .= "AND o.idoffer NOT IN (SELECT idassigned FROM student_offer)\n";
$offersSQL .= "AND o.offertype NOT LIKE 'R'\n";
$offersSQL .= "ORDER BY cn.name, o.idoffer";
$offers = $db->query2array($offersSQL);

$studyfieldSQL = "SELECT\n";
$studyfieldSQL .= "studyfield,\n";
$studyfieldSQL .= "description\n";
$studyfieldSQL .= "FROM studyfield\n";
$studyfieldSQL .= "WHERE timedeleted IS NULL AND official = 'Y'\n";
$studyfieldSQL .= "ORDER BY studyfield";
$studyfield = $db->query2array($studyfieldSQL);
    

$stuyfieldOfferSQL = "SELECT\n";
$stuyfieldOfferSQL .= "idoffer,\n";
$stuyfieldOfferSQL .= "studyfield\n";
$stuyfieldOfferSQL .= "FROM offer_studyfield";
$studyfieldOffer = $db->query2array($stuyfieldOfferSQL);

$tmp = Array();
foreach($studyfieldOffer as $sfo) {
    $tmp[$sfo['idoffer']][] = $sfo['studyfield'];
}
$studyfieldOffer = $tmp;
unset($tmp);

$template = newPage('offers');
$template->title = 'Offers - IAESTE Application System';
//$template->favorites = getFavorites();
$template->jobOffers = $offers;
$template->offerStudyfields = $studyfieldOffer;
//$template->errors = $error;
//$template->fav = true;
$template->l = $l;

try {
    echo $template->execute();
} catch (Exception $e) {
    echo $e;
}
?>
